<template>
  <div style="margin: 15px">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" v-model="searchValue" placeholder="请输入广告名称"/>
      <Button @click="search" type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加广告</Button>
    </div>
    <Table border :columns="adTitle" :data="adList"></Table>
    <page-com :total="total" @page-change="pageChange"></page-com>
    <Modal v-model="identity" width="1200" :title="modalTitle" @on-ok="submit" @on-cancel="cancel">
      <Form :model="formCon" :label-width="80">
        <Row>
          <Col span="8">
            <FormItem label="广告名称">
              <Input v-model="formCon.bannerName" placeholder="请输入广告名称"/>
            </FormItem>
          </Col>
          <Col span="16">
            <FormItem label="跳转路径">
              <Input v-model="formCon.bannerUrl" placeholder="请输入网页跳转路径"/>
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span="24">
            <FormItem label="广告描述">
              <Input v-model="formCon.remark" type="textarea" :autosize="{minRows: 2,maxRows: 3}" placeholder="请输入广告描述"/>
            </FormItem>
          </Col>
        </Row>
        <FormItem label="活动图片">
          <div class="demo-upload-list" v-for="item in uploadList">
            <template v-if="item.status === 'finished'">
              <img :src="item.url">
              <div class="demo-upload-list-cover">
                <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
                <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
              </div>
            </template>
            <template v-else>
              <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
            </template>
          </div>
          <Upload
            ref="upload"
            :show-upload-list="false"
            :on-success="handleSuccess"
            :format="['jpg','jpeg','png']"
            :before-upload="handleBeforeUpload"
            multiple
            type="drag"
            accept="image/*"
            action="//47.110.52.83:8080/api/picture/upload"
            style="display: inline-block;width:78px;">
            <div style="width: 78px;height:78px;line-height: 78px;">
              <Icon type="ios-camera" size="20"></Icon>
            </div>
          </Upload>
          <Modal title="查看大图" v-model="visible">
            <img :src="imgName" v-if="visible" style="width: 100%">
          </Modal>
        </FormItem>
        <FormItem label="设置禁启">
          <i-switch size="large" v-model="formCon.status"  @on-change="statusChange">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import {formatDate} from '_com/date'
  import PageCom from '@/components/page'
  export default {
    name: 'ad-list',
    components: {
      PageCom
    },
    data () {
      return {
        identity: false,
        modalTitle: '',
        searchValue: '',
        pic: [],
        imgName: '',
        visible: false,
        uploadList: [],
        number: 0,
        size: 10,
        total: 0,
        formCon: {
          status: true
        },
        adTitle: [
          {
            title: '广告名称',
            align: 'center',
            tooltip: true,
            key: 'bannerName'
          },
          {
            title: '广告描述',
            align: 'center',
            tooltip: true,
            key: 'remark'
          },
          {
            title: '跳转路径',
            align: 'center',
            tooltip: true,
            key: 'bannerUrl'
          },
          {
            title: '广告图片',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('img', {
                props: {},
                attrs: {src: params.row.pictureUrl},
                style: {
                  width: '90px',
                  height: '90px',
                  margin: '5px auto'
                },
                on: {
                  click: () => {
                    let row = params.row
                    this.handleView(row.pictureUrl)
                  }
                }
              })
            }
          },
          {
            title: '添加人',
            align: 'center',
            tooltip: true,
            key: 'createName'
          },
          {
            title: '添加时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '修改人',
            align: 'center',
            tooltip: true,
            key: 'updateName'
          },
          {
            title: '修改时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.updateTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '状态',
            key: 'status',
            width: 120,
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.status === '1' ? 'primary' : row.status === '0' ? 'error' : ''
              const text = row.status === '1' ? '正常' : row.status === '0' ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      let row = params.row
                      this.formCon.status = Boolean(row.status === '1' ? 1 : 0)
                      this.formCon.bannerName = row.bannerName
                      this.formCon.remark = row.remark
                      this.formCon.bannerUrl = row.bannerUrl
                      this.formCon.id = row.id
                      this.pic = [row.pictureUrl]
                      this.uploadList = [{url: row.pictureUrl, status: 'finished'}]
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.remove(params.row.id)
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        adList: []
      }
    },
    mounted() {
      this.adPage()
    },
    methods: {
      adPage() {
        this.$get('/api/api/org/banner/listPage', {
          bannerName: this.searchValue,
          number: this.number,
          size: this.size
        }).then((res) => {
          if (res.code === 0) {
            this.adList = res.data
            this.total = res.total
          }
        })
      },
      title(title) {
        this.modalTitle = title
        this.identity = true
      },
      add() {
        this.title('添加广告')
      },
      editor() {
        this.title('编辑广告')
      },
      submit() {
        if (this.modalTitle === '添加广告') {
          this.theAdd()
        } else {
          this.theEditor()
        }
      },
      theAdd() {
        this.formCon.status = Number(this.formCon.status)
        this.formCon.pictureUrl = this.pic.join(',')
        this.$post('/api/api/admin/banner/add', this.formCon).then((res) => {
          if (res.code === 0) {
            this.$Message.success('添加成功')
            this.cancel()
            this.adPage()
          } else {
            this.$Message.error('添加失败')
          }
        }).catch((res) => {
          this.$Message.error('添加失败')
        })
      },
      theEditor() {
        this.formCon.status = Number(this.formCon.status)
        this.formCon.pictureUrl = this.pic.join(',')
        this.$put('/api/api/admin/banner/update', this.formCon).then((res) => {
          if (res.code === 0) {
            this.$Message.success('修改成功')
            this.cancel()
            this.adPage()
          } else {
            this.$Message.error('修改失败')
          }
        }).catch((res) => {
          this.$Message.error('修改失败')
        })
      },
      remove(id) {
        this.$deletes('/api/api/admin/banner/delete', {bannerId: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('删除成功')
            this.adPage()
          } else {
            this.$Message.error('修改失败')
          }
        }).catch((res) => {
          this.$Message.error('修改失败')
        })
      },
      search() {
        this.adPage()
      },
      cancel() {
        this.formCon = {}
        this.formCon.status = true
        this.pic = []
        this.uploadList = []
      },
      pageChange(data) {
        this.number = data + 1
        this.adPage()
      },
      handleView (url) {
        this.imgName = url
        this.visible = true
      },
      handleRemove (file) {
        const fileList = this.$refs.upload.fileList
        this.$refs.upload.fileList.splice(fileList.indexOf(file), 1)
        let url = '/api/api/picture/delete'
        let fileUrl = {
          fileUrl: file.url
        }
        this.$deletes(url, fileUrl).then((res) => {
          if (res.code === 0) {
            this.pic.splice(this.pic.indexOf(file), 1)
            this.uploadList.splice(this.uploadList.indexOf(file), 1)
            this.$Message.success('删除成功')
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      handleSuccess (res, file) {
        file.url = file.response.data
        this.$Message.success('上传成功')
        this.pic.push(file.response.data)
        this.uploadList.push(file)
      },
      handleBeforeUpload () {
        const check = this.uploadList.length < 1
        if (!check) {
          this.$Message.error('最多上传1张图片')
        }
        return check
      },
      statusChange(e) {
        this.formCon.status = e
      }
    }
  }
</script>

<style scoped>
  .demo-upload-list{
    display: inline-block;
    width: 80px;
    height: 80px;
    text-align: center;
    line-height:80px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list img{
    width: 100%;
    height: 100%;
  }
  .demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list:hover .demo-upload-list-cover{
    display: block;
  }
  .demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
</style>
